package com.MSofLectures.Operator.Lecture;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;

import com.MSofLectures.Entity.Lecture.Lecture;

public class OperatorOnLecture extends com.MSofLectures.Operator.Basic implements OperatorOnLectureImp{
	private static final long serialVersionUID = 1L;
	/**
	 * @Title OperatorOnLecture.java
	 * @Package com.Entity.Lecture
	 * @author ufo
	 */
	public List<String> showLectures(String max, String first) {
		// 给学生展示众多讲座~
		List<?> list = this.getDatabaseControler().hibernateSess
				.createQuery("from Lecture order by lectureId desc")
				.setMaxResults(Integer.parseInt(max))
				.setFirstResult(Integer.parseInt(first)).list();
		List<String> target = new ArrayList<String>();
		Iterator<?> i = list.iterator();
		while (i.hasNext()) {
			com.MSofLectures.DAO.Lecture lecture = (com.MSofLectures.DAO.Lecture) i.next();
			target.add(lecture.getLectureId());
		}
		return target;
	}

	public Lecture newLecture(String lectureDescription,
			String lecture_submittorId) {
		// 创建新讲座
		com.MSofLectures.DAO.Lecture lecture = new com.MSofLectures.DAO.Lecture();
		try {
			lecture.setLecture_submittorId(lecture_submittorId);
			lecture.setLectureDescription(lectureDescription);
			this.getDatabaseControler().save(lecture);
			this.getDatabaseControler().commit();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return new Lecture(lecture.getLectureId(), lectureDescription,
				lecture_submittorId);
	}

	public Lecture loadLecture(String lectureId) {
		// 载入讲座
		try {
			com.MSofLectures.DAO.Lecture lecture = (com.MSofLectures.DAO.Lecture) this.getDatabaseControler().load(
					com.MSofLectures.DAO.Lecture.class, lectureId);
			String lectureDescription=null;
			String lecture_submittorId=null;
			if (null != lecture){
				lectureDescription=lecture.getLectureDescription();
				lecture_submittorId=lecture.getLecture_submittorId();
			}
				return new Lecture(lectureId, lectureDescription,lecture_submittorId);
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			return null;
		}
	}

	public List<String> iteratorLectures(int max, int first) {
		List list = this.getDatabaseControler().hibernateSess
				.createQuery(
						"select lectureId from Lecture order by lectureId desc")
				.setMaxResults(max).setFirstResult(first).list();
		return list;
	}

	public Integer countLectures() {
		Integer target=Integer.parseInt(this.getDatabaseControler().hibernateSess
				.createQuery("select count(lectureId) from Lecture").list()
				.iterator().next().toString());
		return target;
	}

	public Integer countUsers(String lectureId) {
		Integer target=Integer
				.parseInt(this.getDatabaseControler().hibernateSess
						.createQuery(
								"select count(userId) from User_registerLecture where lectureId=:lectureId")
						.setString("lectureId", lectureId).list().iterator()
						.next().toString());
		return target;
	}

	public Integer countcheckUsers(String lectureId) {
		try {
			Integer target=Integer
					.parseInt(this.getDatabaseControler().hibernateSess
							.createQuery(
									"select count(userId) from User_registerLecture where lectureId=:lectureId and lecture_registerCodeId is not null")
							.setString("lectureId", lectureId).list()
							.iterator().next().toString());
			return target;
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
	}
}
